package com.example.criminalintent.model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.criminalintent.db.CrimeBaseHelper;
import com.example.criminalintent.db.CrimeCursorWrapper;
import com.example.criminalintent.db.CrimeDbSchema;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class CrimeLab {

    private static CrimeLab sCrimeLab;

    //private List<Crime> mCrimes;

    private Context context;
    private SQLiteDatabase mDatabase;

    private CrimeLab(Context context){
      //  mCrimes = new ArrayList<>();
        this.context = context;
        mDatabase = new CrimeBaseHelper(context).getWritableDatabase();

        /*for(int i = 0;i<100;i++){
            Crime crime = new Crime();
            crime.setmTitle("Crime #"+i);
            crime.setmSolved(i%2==0);
            mCrimes.add(crime);
        }*/
    }
    private CrimeLab(){}
    public static CrimeLab getsCrimeLab(Context context){
        if(sCrimeLab == null){
            sCrimeLab = new CrimeLab(context);
        }
        return sCrimeLab;
    }

    public List<Crime> getCrimes(){
        //return mCrimes;
        List<Crime> crimes = new ArrayList<>();
        CrimeCursorWrapper cursorWrapper = queryCrimes(null,null);
        try{
            cursorWrapper.moveToFirst();
            while (!cursorWrapper.isAfterLast()){
                crimes.add(cursorWrapper.getCrime());
                cursorWrapper.moveToNext();
            }
        }finally {
            cursorWrapper.close();
        }
        return crimes;
    }

    public Crime getCrime(UUID uuid){
//        for(Crime crime:mCrimes){
//            if(crime.getmId().equals(uuid)){
//                return crime;
//            }
//        }
        CrimeCursorWrapper cursorWrapper = queryCrimes(
                CrimeDbSchema.CrimeTable.Cols.UUID + "= ?",
                new String[]{uuid.toString()});

        try{
            if(cursorWrapper.getCount() == 0){
                return null;
            }
            cursorWrapper.moveToFirst();
            return cursorWrapper.getCrime();
        }finally {
            cursorWrapper.close();
        }

    }

    public void addCrime(Crime c){
        //mCrimes.add(c);
        ContentValues values = getContentValues(c);
        mDatabase.insert(CrimeDbSchema.CrimeTable.NAME,null,values);
    }

    public void removeCrime(Crime crime){
        mDatabase.delete(CrimeDbSchema.CrimeTable.NAME,CrimeDbSchema.CrimeTable.Cols.UUID+"= ?"
                ,new String[]{crime.getmId().toString()});
    }

    private static ContentValues getContentValues(Crime crime){
        ContentValues values = new ContentValues();
        values.put(CrimeDbSchema.CrimeTable.Cols.UUID, crime.getmId().toString());
        values.put(CrimeDbSchema.CrimeTable.Cols.TITLE, crime.getmTitle());
        values.put(CrimeDbSchema.CrimeTable.Cols.DATE, crime.getmDate().getTime());
        values.put(CrimeDbSchema.CrimeTable.Cols.SOLVED, crime.ismSolved() ? 1 : 0);
        values.put(CrimeDbSchema.CrimeTable.Cols.SUSPECT,crime.getmSuspect());
        return values;
    }

    public void updateCrime(Crime crime){
        ContentValues contentValues = getContentValues(crime);
        String uuidString = crime.getmId().toString();
        mDatabase.update(CrimeDbSchema.CrimeTable.NAME,contentValues, CrimeDbSchema.CrimeTable.Cols.UUID
        +"= ?",new String[]{uuidString});
    }


//    private Cursor queryCrimes(String whereClause,String[] whereArgs){
//        Cursor cursor = mDatabase.query(
//                CrimeDbSchema.CrimeTable.NAME,
//                null,
//                whereClause,
//                whereArgs,
//                null,
//                null,
//                null
//        );
//        return cursor;
//    }


    private CrimeCursorWrapper queryCrimes(String whereClause,String[] whereArgs){
        Cursor cursor = mDatabase.query(
                CrimeDbSchema.CrimeTable.NAME,
                null,
                whereClause,
                whereArgs,
                null,
                null,
                null);
            return new CrimeCursorWrapper(cursor);
    }




}
